//
//  BYTreeNodeData.m
//  BYTreeView
//
//  Created by Bau-yin on 16/1/6.
//  Copyright (c) 2016年 Bau-yin. All rights reserved.
//

#import "BYTreeNodeData.h"

@interface BYTreeNodeData ()
/// 层级
@property (nonatomic) NSUInteger level;
/// 孩子节点
@property (nonatomic) NSMutableArray *child;
/// 父节点
@property (nonatomic, weak) BYTreeNodeData *parent;
/// 兄弟节点
@property (nonatomic, weak) BYTreeNodeData *brother;
@end

@implementation BYTreeNodeData

#pragma mark - init
- (instancetype)init
{
    if (self = [super init])
    {
        self.expand = NO;
        self.level = 0;
        self.child = [NSMutableArray array];
        self.parent = nil;
        self.brother = nil;
    }
    return self;
}

- (instancetype)initWithDataModel:(id)dataModel
{
    if (self = [self init])
    {
        self.dataModel = dataModel;
    }
    return self;
}

#pragma mark - Getter & Setter
- (NSUInteger)nodeCount
{
    NSUInteger count = 1;
    if (self.isExpand)
    {
        for (BYTreeNodeData* node in self.child)
        {
            count += node.nodeCount;
        }
    }   
    return count;
}

- (void)setIsSelected:(BOOL)isSelected
{
    _isSelected = isSelected;
    for (BYTreeNodeData *nodeData in self.child)
    {
        nodeData.isSelected = _isSelected;
    }
}

#pragma mark - Methods
- (BYTreeNodeData *)nextNode
{
    if (self.isExpand && self.child.count != 0)
    {
        return self.child[0];
    }
    else
    {
        for (BYTreeNodeData *node = self; node != nil; node = node.parent)
        {
            if (node.brother != nil)
            {
                return node.brother;
            }
        }
        return nil;
    }
}

- (NSArray *)nodeArray:(BOOL)includeRoot
{
    NSMutableArray *array = [NSMutableArray array];
    if (includeRoot)
    {
        [array addObject:self];
        for (BYTreeNodeData *node = [self nextNode]; node != nil && node.level > self.level; node = [node nextNode])
        {
            [array addObject:node];
        }
    }
    else
    {
        for (BYTreeNodeData *node = self.child[0]; node != nil && node.level > self.level; node = [node nextNode])
        {
            [array addObject:node];
        }
    }
    return [NSArray arrayWithArray:array];
}

- (void)insertChild:(BYTreeNodeData *)node
{
    node.level = self.level + 1;
    node.parent = self;
    node.brother = nil;
    if (self.child.count > 0) {
        ((BYTreeNodeData*)self.child[self.child.count - 1]).brother = node;
    }
    [self.child addObject:node];
}

- (void)insertChild:(BYTreeNodeData *)node index:(NSUInteger)indexes
{
    if (indexes >= self.child.count)
    {
        indexes = self.child.count - 1;
    }
    node.level = self.level + 1;
    node.parent = self;
    node.brother = self.child[indexes];
    if (indexes > 0)
    {
        ((BYTreeNodeData*)self.child[indexes - 1]).brother = node;
    }
    [self.child insertObject:node atIndex:indexes];
}

- (BOOL)removeChild:(BYTreeNodeData *)node
{
    for (int i = 0; i < self.child.count; i++)
    {
        if ([node isEqual:self.child[i]])
        {
            return [self removeChildAtIndexes:i];
        }
    }
    return NO;
}

- (BOOL)removeChildAtIndexes:(NSUInteger)indexes
{
    if (indexes < self.child.count)
    {
        if (indexes > 0)
        {
            if (indexes < self.child.count - 1)
            {
                ((BYTreeNodeData*)self.child[indexes - 1]).brother = self.child[indexes + 1];
            }
            else
            {
                ((BYTreeNodeData*)self.child[indexes - 1]).brother = nil;
            }
        }
        [self.child removeObjectAtIndex:indexes];
        return YES;
    }
    else
    {
        return NO;
    }
}

- (BYTreeNodeData *)rootNode
{
    BYTreeNodeData *node = self;
    while (node.parent != nil)
    {
        node = node.parent;
    }
    return node;
}

- (void)clear
{
    [self.child removeAllObjects];
}
@end
